#include <GameLogic/Include/GameLogic.h>

#include <windows.h>
#include "resource.h"
#include <CommCtrl.h>

#include <SnakeGame/WinApp.h>
#include <SnakeGame/WinWindow.h>

#include <vld.h>

GameLogic* game_logic;
GameLogic::GameRenderingPlatform rendering_platform_chosen = GameLogic::GameRenderingPlatform::RENDER_WITH_DX9;
int scene_size = 12;
int snake_velocity = 2;

// -------------------------------------------------------------------------
LRESULT CALLBACK setup_dialog_handler(HWND hDlg,UINT wm,WPARAM wp,LPARAM lp)
{
	static HWND hlist;
	switch(wm)
	{
	case WM_INITDIALOG:
		{
            SendDlgItemMessage(hDlg,IDC_OPTION_DX9,BM_SETCHECK,BST_CHECKED,0);
            
            SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETRANGE, (WPARAM)1, (LPARAM)MAKELONG(10, 50));

            SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETTICFREQ, 1, 5);

            SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETPOS, 1, 12);

            SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_SETRANGE, (WPARAM)1, (LPARAM)MAKELONG(1, 10));

            SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_SETTICFREQ, 1, 2);

            SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_SETPOS, 1, 5);
			return true;
		}
		break;
	case WM_COMMAND:
		switch(wp)
		{
			case IDOK:
			{				
				int checked;
				// ventana
				checked = SendDlgItemMessage(hDlg,IDC_OPTION_DX9,BM_GETCHECK,0,0);
				if( checked == BST_CHECKED )
				{
					rendering_platform_chosen = GameLogic::GameRenderingPlatform::RENDER_WITH_DX9;
				}

				checked = SendDlgItemMessage(hDlg,IDC_OPTION_DX11,BM_GETCHECK,0,0);
				if( checked == BST_CHECKED )
				{
					rendering_platform_chosen = GameLogic::GameRenderingPlatform::RENDER_WITH_DX11;
                }

                scene_size = SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_GETPOS, (WPARAM)0, (LPARAM)0);
                snake_velocity = SendDlgItemMessage(hDlg, IDC_SLIDER2, TBM_GETPOS, (WPARAM)0, (LPARAM)0);

				EndDialog(hDlg,true);
				return 1;
			}
			break;
			case IDCANCEL:
			{
				EndDialog(hDlg,false);
				MessageBox(hDlg,L"Oh! You have chosen to quit this game!",L"Quit From Game",MB_ICONEXCLAMATION);
				return 1;
			}
			break;            
		}		
		break;
	}
	
	return 0;
};


//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
                             WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
				   LPSTR lpCmdLine, int nCmdShow)
{
    DLGPROC dlgproc;
    dlgproc=(DLGPROC)setup_dialog_handler;
    bool goon = (bool)DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,dlgproc);

    if( !goon)
    {
        return 0;
    }

    MSG   Msg;
	LPCTSTR ClsName = L"Win32OOP";
	LPCTSTR WndName = L"Snake Game";

	// Initialize the application class
	WinApp* winApp = new WinApp(hInstance, ClsName, MainWndProc);
	winApp->Register();

	// Create the main window
	int win_width = 500;
	int win_height = 500;
	WinWindow* wnd = new WinWindow();
	wnd->Create(hInstance, ClsName, WndName, NULL, WS_OVERLAPPEDWINDOW , 0, 0, 0, win_width, win_height);
	
	// Create auxi window for opengl setup
	WinWindow* auxi_wnd = new WinWindow();
	auxi_wnd->Create(hInstance, ClsName, WndName, NULL, WS_OVERLAPPEDWINDOW , 0, 0, 0, 800, 600);
	// Don't show the auxi window.
	ShowWindow(auxi_wnd->getHWND(), SW_HIDE);
	
    // Display the main window
	wnd->Show();
	
    game_logic = new GameLogic(wnd, rendering_platform_chosen, auxi_wnd);

    game_logic->load(scene_size, snake_velocity);
    game_logic->run();
	
	// Process the main window's messages
	/*while( GetMessage(&Msg, NULL, 0, 0) )
	{
    	TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}*/
	while(1)
	{
		if( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
		else
		{
			GameLogic::main_task_function(game_logic);
		}
	}

    game_logic->unload();
    delete game_logic;

	delete winApp;
	delete wnd;
	//DestroyWindow(auxi_wnd->getHWND());

	return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
    case WM_CLOSE:
	case WM_DESTROY:
        if (game_logic->hasTheGameFinished())
        {
            PostQuitMessage(WM_QUIT);
        }
        else
        {
            MessageBox(hWnd,L"Oh! You have to wait till game is finished!",L"Cannot Quit From Game",MB_ICONEXCLAMATION);
        }
		return 0;
        break;
    case WM_KEYDOWN:
        {
            switch (wParam)
            {
            case VK_RIGHT:
                game_logic->input(GameLogic::GameInputType::RIGHT_ARROW_PRESSED);
                break;
            case VK_LEFT:
                game_logic->input(GameLogic::GameInputType::LEFT_ARROW_PRESSED);
                break;
            case VK_SPACE:
                game_logic->changeView();
                break;
            }            
        }
        break;    
	}

	return DefWindowProc(hWnd, Msg, wParam, lParam);
}
//-----------------------------------------------




